<template>
  <div class="postslist">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card" style="margin-top:20px">
      <!-- 表格 -->
      <!-- 1.制作表格的静态结构就是拼装的过程 -->
      <!-- 2.凡是有数据相关的操作,都需要首先考虑到自定义列表 -->
      <el-table :data="postsList" style="width: 100%" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="title" label="标题" width="500"></el-table-column>
        <el-table-column prop="type" label="类型" width="180">
          <template slot-scope="scope">{{scope.row.type==1?'文章':'视频'}}</template>
        </el-table-column>
        <el-table-column prop="user.nickname" label="作者"></el-table-column>
        <!-- 通过 Scoped slot 可以获取到 row, column,
        $index 和 store（table 内部的状态管理）的数据，用法参考 demo。-->
        <!-- 凡是涉及到数据的相关操作(新增，删除，修改，查询)，都应该添加自定义模板列 -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[2, 4, 6, 8,]"
        :page-size="2"
        layout="total, sizes, prev, pager, next, jumper"
        :total="6"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getPostsList } from "@/apis/articles.js";
export default {
  data() {
    return {
      postsList: [],
      pageSize: 6, //每页显示的数量
      pageIndex: 1, //当前页码
      //   total: 0,
    };
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      //   1.修改全局变量的值
      this.pageSize = val;
      //   2.根据全局变量的值再次发起请求
      this.init();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      //   1.修改全局变量的值
      this.pageIndex = val;
      //   2.根据全局变量的值再次发起请求
      this.init();
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    async init() {
      let res = await getPostsList({
        pageSize: this.pageSize,
        pageIndex: this.pageIndex,
      });
      console.log(res);
      this.postsList = res.data.data;
      //   this.total = res.data.total;
    },
  },
  async mounted() {
    this.init();
  },
};
</script>

<style lang="less" scoped>
</style>